// page/component/new-pages/user/address/address.js
Page({
  data: {
    address: {
      name: '',
      phone: '',
      detail: ''
    }
  },
  onLoad() {
    var self = this;

    wx.getStorage({
      key: 'address',
      success: function (res) {
        self.setData({
          address: res.data
        })
      }
    })
  },
  formSubmit(e) {
    const value = e.detail.value;
    if (value.name && value.phone && value.detail) {
      wx.setStorage({
        key: 'address',
        data: value,
        success() {
          wx.navigateBack();
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请填写完整资料',
        showCancel: false
      })
    }
  },
  getName(e) {
    console.log(e.detail.value)
    var phone = this.data.address.phone
    var detail = this.data.address.detail
    this.setData({
      address: {
        name: e.detail.value,
        phone,
        detail
      }
    })
  },
  getPhone(e) {
    console.log(e.detail.value)
    var name = this.data.address.name
    var detail = this.data.address.detail
    this.setData({
      address: {
        phone: e.detail.value,
        name,
        detail
      }
    })
  },
  getAd(e){
    console.log(e.detail.value)
    var name = this.data.address.name
    var phone = this.data.address.phone
    this.setData({
      address: {
        phone,
        name,
        detail:e.detail.value
      }
    })
  },
  getAddr() {
    //获取用户位置信息并保存
    wx.getLocation({
      type: 'wgs84',
      altitude: false,
      success: (res) => {
        var {
          latitude,
          longitude
        } = res
        var key = "IMRBZ-PTUCU-XOYVI-2CJBM-CPTXK-LVFIC"
        var url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${key}&get_poi=1`
        wx.request({
          url: url,
          success: res => {
            var address = res.data.result.address
            console.log(address)
            wx.setStorageSync('address', address)
            var name = this.data.address.name
            var phone = this.data.address.phone
            console.log(name)
            console.log(phone) 
            this.setData({
              address: {
                detail: address,
                name,
                phone
              }
            })
          }
        })
      }
    })
  }
})